package com.cyan.app.workflow.common;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.cyan.app.workflow.AppContext;

import org.json.JSONObject;
import org.json.JSONTokener;

public class UserConfiger {

    public static final String USER_CONFIG_FILE_NAME = "userNamesFile";

    public static class UserLocalData {
        public String userID = "";
        public String userType = "";
        public String userName = "";
        public String userToken = "";
        public  String userConnect = "";

        public UserLocalData() {
            clear();
        }

        public void clear() {
            userID = "";
            userType = "";
            userName = "";
            userToken = "";
            userConnect ="";
        }

        public boolean isEmpty() {
            return TextUtils.isEmpty(userID) || TextUtils.isEmpty(userName)
                    || TextUtils.isEmpty(userToken) || TextUtils.isEmpty(userType);
        }
    }

    ;

    public static void clear(SharedPreferences sp) {
        if (sp == null) {
            return;
        }
        Editor edit = sp.edit();
        edit.putString("last-usertoken", "");
        edit.putString("last-userid", "");
        edit.commit();
    }

    public static void clear(Context context) {
        if (context == null) {
            return;
        }
        SharedPreferences sp = context.getSharedPreferences(USER_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
        clear(sp);
    }

    public static UserLocalData parseJsonString(String jsonString) {
        UserLocalData userData = new UserLocalData();
        if (jsonString == null) {
            return userData;
        }
        try {
            JSONTokener jsonDataParser = new JSONTokener(jsonString);
            JSONObject jsData = (JSONObject) jsonDataParser.nextValue();
            userData.userID = jsData.getString("id");
            userData.userName = jsData.getString("name");
            userData.userToken = jsData.getString("token");
            userData.userType = jsData.getString("usertype");
            userData.userConnect = jsData.getString("userconnect");
            return userData;
        } catch (Exception e) {
            e.printStackTrace();
            return userData;
        }
    }

    public static void save(Context context, UserLocalData data) {
        if (context == null) {
            return;
        }
        SharedPreferences sp = context.getSharedPreferences(USER_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
        save(sp, data);
    }

    public static void save(SharedPreferences sp, UserLocalData data) {
        if (sp == null || data == null) {
            return;
        }
        Editor edit = sp.edit();
        edit.putString("last-usertoken", data.userToken);
        edit.putString("last-usertype", data.userType);
        edit.putString("last-username", data.userName);
        edit.putString("last-userid", data.userID);
        edit.putString("last-userconnect", data.userConnect);
        edit.putString(data.userName, "");
        edit.commit();
    }

    public static UserLocalData read(Context context) {
        if (context == null) {
            return new UserLocalData();
        }
        SharedPreferences sp = context.getSharedPreferences(USER_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
        return read(sp);
    }

    public static UserLocalData read(SharedPreferences sp) {
        UserLocalData userData = new UserLocalData();
        if (sp == null) {
            return userData;
        }
        userData.userToken = sp.getString("last-usertoken", "");
        userData.userType = sp.getString("last-usertype", "");
        userData.userName = sp.getString("last-username", "");
        userData.userID = sp.getString("last-userid", "");
        userData.userConnect = sp.getString("last-userconnect", "");
        return userData;
    }

    public static SharedPreferences getSharedPref() {
        return AppContext.getInstance().getSharedPreferences(USER_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
    }
}